package com.ruoyi.buildingacidsys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

import com.ruoyi.basicsys.domain.BasicHousehold;
import com.ruoyi.basicsys.service.IBasicHouseholdService;
import com.ruoyi.householdacidsys.domain.HouseholdNucleicAcid;
import com.ruoyi.householdacidsys.service.IHouseholdNucleicAcidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buildingacidsys.mapper.BuildingNucleicAcidMapper;
import com.ruoyi.buildingacidsys.domain.BuildingNucleicAcid;
import com.ruoyi.buildingacidsys.service.IBuildingNucleicAcidService;

/**
 * 楼栋核酸任务Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-28
 */
@Service
public class BuildingNucleicAcidServiceImpl extends ServiceImpl<BuildingNucleicAcidMapper, BuildingNucleicAcid> implements IBuildingNucleicAcidService {
    @Autowired
    private BuildingNucleicAcidMapper buildingNucleicAcidMapper;
    @Autowired
    private IBasicHouseholdService basicHouseholdService;
    @Autowired
    private IHouseholdNucleicAcidService householdNucleicAcidService;

    /**
     * 查询楼栋核酸任务
     *
     * @param id 楼栋核酸任务主键
     * @return 楼栋核酸任务
     */
    @Override
    public BuildingNucleicAcid selectBuildingNucleicAcidById(Long id) {
        return buildingNucleicAcidMapper.selectBuildingNucleicAcidById(id);
    }

    /**
     * 查询楼栋核酸任务列表
     *
     * @param buildingNucleicAcid 楼栋核酸任务
     * @return 楼栋核酸任务
     */
    @Override
    public List<BuildingNucleicAcid> selectBuildingNucleicAcidList(BuildingNucleicAcid buildingNucleicAcid) {
        return buildingNucleicAcidMapper.selectBuildingNucleicAcidList(buildingNucleicAcid);
    }

    /**
     * 新增楼栋核酸任务
     *
     * @param buildingNucleicAcid 楼栋核酸任务
     * @return 结果
     */
    @Override
    public int insertBuildingNucleicAcid(BuildingNucleicAcid buildingNucleicAcid) {
        HouseholdNucleicAcid householdNucleicAcid = new HouseholdNucleicAcid();
        //在添加之前去查询小区的楼栋有多少人，然他把他设置到任务人数中
        //查询每个小区每栋楼有多少人
        List<BasicHousehold> basicHouseholds = basicHouseholdService.selectTaskPeople();
        //循环小区楼栋人数
        for (BasicHousehold b:basicHouseholds) {
            //如果楼栋核酸任务表传过来的名字和楼栋和住户表的一样（就直接添加完成人数）
            if(buildingNucleicAcid.getCommunityName().equals(b.getCommunityName())
                    &&buildingNucleicAcid.getBuildingName().equals(b.getBuildingName())){
                //用前端发布过来的小区名字和楼栋去找有多少个用户，并把值加进去
                buildingNucleicAcid.setTaskPeople(Long.valueOf(b.getCountHousehold()));
                //在发布任务的时候也会给住户核算任务发布任务，所以我们先去查询这个小区这栋楼有哪些用户
                List<BasicHousehold> HouseholdsList = basicHouseholdService.selectHouseholdList(b);
                for (BasicHousehold household:HouseholdsList) {
                    //设置楼栋名字
                    householdNucleicAcid.setBuildingName(household.getBuildingName());
                    //小区
                    householdNucleicAcid.setCommunityName(household.getCommunityName());
                    //名字
                    householdNucleicAcid.setHouseholdName(household.getHouseholdName());
                    //发布任务 住户状态就设置为进行中
                    householdNucleicAcid.setStatus("1");
                    //设置开始时间
                    householdNucleicAcid.setBeginTime(buildingNucleicAcid.getBeginTime());
                    //设置任务名
                    householdNucleicAcid.setTaskName(buildingNucleicAcid.getTaskName());
                    //设置检测位置
                    householdNucleicAcid.setDetectionLocation(buildingNucleicAcid.getDetectionLocation());
                    //设置结束时间
                    householdNucleicAcid.setEndTime(buildingNucleicAcid.getEndTime());
                    //当前检测时间
                    householdNucleicAcid.setDetectTime(new Date());
                    //添加住户核算信息
                    householdNucleicAcidService.insertHouseholdNucleicAcid(householdNucleicAcid);
                }

            }
        }
        return buildingNucleicAcidMapper.insertBuildingNucleicAcid(buildingNucleicAcid);
    }

    /**
     * 修改楼栋核酸任务
     *
     * @param buildingNucleicAcid 楼栋核酸任务
     * @return 结果
     */
    @Override
    public int updateBuildingNucleicAcid(BuildingNucleicAcid buildingNucleicAcid) {
        return buildingNucleicAcidMapper.updateBuildingNucleicAcid(buildingNucleicAcid);
    }

    /**
     * 批量删除楼栋核酸任务
     *
     * @param ids 需要删除的楼栋核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteBuildingNucleicAcidByIds(Long[] ids) {
        return buildingNucleicAcidMapper.deleteBuildingNucleicAcidByIds(ids);
    }

    /**
     * 删除楼栋核酸任务信息
     *
     * @param id 楼栋核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteBuildingNucleicAcidById(Long id) {
        return buildingNucleicAcidMapper.deleteBuildingNucleicAcidById(id);
    }

    @Override
    public List<BuildingNucleicAcid> selectAllBuildingNucleicAcid() {
        return buildingNucleicAcidMapper.selectAllbuildingNucleicAcid();
    }
}